<!DOCTYPE html>
<html lang="en">





<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="My Personal Website For Blog">
  <meta name="author" content="黄宇辉">
  <meta name="keywords" content="learning note,hexo blog">
  <title>Java web:Filter - 欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/vs2015.min.css" />
  

  


<!-- 主题依赖的图标库，不要自行修改 -->
<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_yg9cfy8wd6.css">

<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">

<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>ANONYMOUS</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                Home
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                Archives
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                Tags
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                About
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="view intro-2" id="background" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container text-center white-text fadeInUp">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2019-05-02 09:29">
      May 2, 2019 am
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.2k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      36
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <div class="post-content mx-auto" id="post">
            
            <article class="markdown-body">
              <h2 id="学习笔记-Java-web之过滤器"><a href="#学习笔记-Java-web之过滤器" class="headerlink" title="学习笔记 : Java web之过滤器"></a>学习笔记 : Java web之过滤器</h2><h3 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h3><p><em><code>Filter</code>被称作过滤器或者拦截器,作为Servlet 2.3中新增的技术,可以实现用户在访问某个目标资源之前,对访问的请求和响应进行相应处理.其基本功能就是对Servlet容器调用<code>Servlet</code>的过程进行拦截,从而在Servlet响应处理前后实现一些特殊功能. Filter过滤器实现了<code>javax.servlet.Filter</code>接口.在Filter接口中定义了三个方法,其代表Filter的生命周期,如下所示 :</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-comment">// 用于初始化过滤器</span>
init(FilterConfig filterConfig);
<span class="hljs-comment">// 客户端请求时被调用(Filter所有的工作都集中在此方法中)</span>
doFilter(ServletRequest request,ServletResponse response,FilterChain chain);
<span class="hljs-comment">// 在Web应用程序卸载时被调用</span>
destory();</code></pre></div>


<h3 id="Filter-annotation"><a href="#Filter-annotation" class="headerlink" title="Filter annotation"></a>Filter annotation</h3><p><em>简介 : <code>Servlet3.0</code>提供<code>@WebFilter注解</code>将一个实现了<code>javax.servlet.Filter</code>接口的类定义为过滤器,继而当我们在Web应用中使用过滤器时无需在web.xml文件中配置Filter映射 !</em></p>
<h4 id="注解示例-拦截用户的所有请求"><a href="#注解示例-拦截用户的所有请求" class="headerlink" title="注解示例-拦截用户的所有请求"></a>注解示例-拦截用户的所有请求</h4><div class="hljs"><pre><code class="hljs java"><span class="hljs-meta">@WebFilter</span>(filterName=<span class="hljs-string">"/MyFilter"</span> , urlPatterns= <span class="hljs-string">"/*"</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyFilter</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Filter</span> </span>&#123;
    ···
&#125;</code></pre></div>

<h4 id="注解示例-拦截不同方式的访问请求"><a href="#注解示例-拦截不同方式的访问请求" class="headerlink" title="注解示例-拦截不同方式的访问请求"></a>注解示例-拦截不同方式的访问请求</h4><p><em>描述 : 根据被Servlet容器调用的方式来拦截指定的资源.其调用方式如下所示 :</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> javax.servlet;

<span class="hljs-comment">/**
 * Enumeration of filter dispatcher types.
 *
 * <span class="hljs-doctag">@since</span> Servlet 3.0
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">enum</span> DispatcherType &#123;
    FORWARD,
    INCLUDE,
    REQUEST,
    ASYNC,
    ERROR
&#125;</code></pre></div>

<p><em>下面以<code>FORWARD</code>(如果目标资源是通过<code>RequestDispatcher</code>的<code>forward()</code>方法访问时,该过滤器将会被调用)为例,演示Filter对转发请求的过滤效果.</em></p>
<ol>
<li><p><em>ServletTest : 将请求转发给index.jsp页面.</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-meta">@WebServlet</span>(<span class="hljs-string">"/ServletTest"</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ServletTest</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">HttpServlet</span> </span>&#123;
	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = <span class="hljs-number">1L</span>;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ServletTest</span><span class="hljs-params">()</span> </span>&#123;
		<span class="hljs-keyword">super</span>();
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doGet</span><span class="hljs-params">(HttpServletRequest request, HttpServletResponse response)</span>
			<span class="hljs-keyword">throws</span> ServletException, IOException </span>&#123;

		request.getRequestDispatcher(<span class="hljs-string">"/index.jsp"</span>).forward(request, response);
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doPost</span><span class="hljs-params">(HttpServletRequest request, HttpServletResponse response)</span>
			<span class="hljs-keyword">throws</span> ServletException, IOException </span>&#123;
		doGet(request, response);
	&#125;

&#125;</code></pre></div>
</li>
<li><p><em>FilterTest.java : 拦截index.jsp页面.</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-comment">// Servlet 3.0</span>
<span class="hljs-meta">@WebFilter</span>(filterName = <span class="hljs-string">"/FilterTest"</span>, urlPatterns = <span class="hljs-string">"/index.jsp"</span>, dispatcherTypes = &#123; DispatcherType.FORWARD &#125;) 																				
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FilterTest</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Filter</span> </span>&#123;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">FilterTest</span><span class="hljs-params">()</span> </span>&#123;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">destroy</span><span class="hljs-params">()</span> </span>&#123;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doFilter</span><span class="hljs-params">(ServletRequest request, ServletResponse response, FilterChain chain)</span>
			<span class="hljs-keyword">throws</span> IOException, ServletException </span>&#123;
		response.getWriter().println(<span class="hljs-string">"This is Filter !"</span>);

	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">(FilterConfig fConfig)</span> <span class="hljs-keyword">throws</span> ServletException </span>&#123;
	&#125;

&#125;</code></pre></div>


</li>
</ol>
<h3 id="FilterConfig-interface"><a href="#FilterConfig-interface" class="headerlink" title="FilterConfig interface"></a>FilterConfig interface</h3><p><em>简介 : 为了获取Filter程序在<code>web.xml</code>中的配置信息,Servlet API提供了一个FilterConfig接口,该接口封装了Filter程序在web.xml中的所有注册信息.并提供了一系列获取这些配置信息的方法.(同时适用于注解的哟 ~)</em></p>
<h4 id="getInitParameter-String-name"><a href="#getInitParameter-String-name" class="headerlink" title="getInitParameter(String name)"></a>getInitParameter(String name)</h4><p><em>描述 : 下面以接口FilterConfig接口中的<code>getInitParameter(String name)</code>方法为例,演示FilterConfig接口的作用.</em></p>
<ol>
<li><p><em>FilterConfigTest.java : 获取注解中指定名称的初始化参数值.</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-comment">// 使用注解为该Filter设置初始化参数: initParams= &#123;@WebInitParam(name="encoding",value="utf-8")&#125;</span>
<span class="hljs-meta">@WebFilter</span>(filterName = <span class="hljs-string">"/FilterConfigTest"</span>, urlPatterns = <span class="hljs-string">"/MyServlet"</span>, initParams = &#123;
		<span class="hljs-meta">@WebInitParam</span>(name = <span class="hljs-string">"encoding"</span>, value = <span class="hljs-string">"utf-8"</span>) &#125;)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FilterConfigTest</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Filter</span> </span>&#123;

	FilterConfig filterConfig;
	String characterEncoding = <span class="hljs-keyword">null</span>;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">FilterConfigTest</span><span class="hljs-params">()</span> </span>&#123;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">destroy</span><span class="hljs-params">()</span> </span>&#123;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doFilter</span><span class="hljs-params">(ServletRequest request, ServletResponse response, FilterChain chain)</span>
			<span class="hljs-keyword">throws</span> IOException, ServletException </span>&#123;

		response.setContentType(<span class="hljs-string">"text/html;charset=utf-8"</span>);

		characterEncoding = filterConfig.getInitParameter(<span class="hljs-string">"encoding"</span>);
		response.getWriter().println(<span class="hljs-string">"encoding的初始化参数的值为: "</span> + characterEncoding);
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">(FilterConfig fConfig)</span> <span class="hljs-keyword">throws</span> ServletException </span>&#123;
		<span class="hljs-keyword">this</span>.filterConfig = fConfig;
	&#125;

&#125;</code></pre></div>
</li>
<li><p><em>FilterConfigTest.java : 获取web.xml中指定名称的初始化参数值(代码同FilterConfigTest.java),web.xml中的配置信息如下 :</em></p>
<div class="hljs"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">web-app</span> <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">"http://www.w3.org/2001/XMLSchema-instance"</span>
	<span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://xmlns.jcp.org/xml/ns/javaee"</span>
	<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"</span>
	<span class="hljs-attr">id</span>=<span class="hljs-string">"WebApp_ID"</span> <span class="hljs-attr">version</span>=<span class="hljs-string">"4.0"</span>&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">display-name</span>&gt;</span>Filter_basic<span class="hljs-tag">&lt;/<span class="hljs-name">display-name</span>&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">welcome-file-list</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">welcome-file</span>&gt;</span>index.html<span class="hljs-tag">&lt;/<span class="hljs-name">welcome-file</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">welcome-file</span>&gt;</span>index.htm<span class="hljs-tag">&lt;/<span class="hljs-name">welcome-file</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">welcome-file</span>&gt;</span>index.jsp<span class="hljs-tag">&lt;/<span class="hljs-name">welcome-file</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">welcome-file</span>&gt;</span>default.html<span class="hljs-tag">&lt;/<span class="hljs-name">welcome-file</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">welcome-file</span>&gt;</span>default.htm<span class="hljs-tag">&lt;/<span class="hljs-name">welcome-file</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">welcome-file</span>&gt;</span>default.jsp<span class="hljs-tag">&lt;/<span class="hljs-name">welcome-file</span>&gt;</span>
	<span class="hljs-tag">&lt;/<span class="hljs-name">welcome-file-list</span>&gt;</span>

	<span class="hljs-tag">&lt;<span class="hljs-name">filter</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">filter-name</span>&gt;</span>FilterConfigTest<span class="hljs-tag">&lt;/<span class="hljs-name">filter-name</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">filter-class</span>&gt;</span>pers.huangyuhui.filter.filterconfig.FilterConfigTest<span class="hljs-tag">&lt;/<span class="hljs-name">filter-class</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span>
			<span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>encoding<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span>
			<span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>gbk2312<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span>
		<span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span>
	<span class="hljs-tag">&lt;/<span class="hljs-name">filter</span>&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">filter-mapping</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">filter-name</span>&gt;</span>FilterConfigTest<span class="hljs-tag">&lt;/<span class="hljs-name">filter-name</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>/MyServlet<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span>
	<span class="hljs-tag">&lt;/<span class="hljs-name">filter-mapping</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">web-app</span>&gt;</span></code></pre></div>


</li>
</ol>
<h3 id="Filter-application"><a href="#Filter-application" class="headerlink" title="Filter application"></a>Filter application</h3><h4 id="Filter-实现用户自动登录"><a href="#Filter-实现用户自动登录" class="headerlink" title="Filter 实现用户自动登录"></a>Filter 实现用户自动登录</h4><p><em>案例描述 : 使用Cookie实现用户自动登录后,当客户端访问服务器的Servlet时,所有的Servelt都需要对用户的Cookie信息进行校验,这样势必会导致在Servlet程序中书写大量重复的代码.为了解决这个问题,可以在Filter程序中实现Cookie的校验.由于Filter可以对服务器的所有请求进行拦截,因此,一旦请求通过Filter程序,就相当于用户信息校验通过,然后Servelt程序就可以根据获取到的用户信息实现自动登录功能了.</em></p>
<ol>
<li><p><em>User.java : 封装用户的信息.</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.filter.example.autologin;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: User
 * <span class="hljs-doctag">@Description</span>: 封装用户的信息
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 1, 2019 7:24:26 PM
 * 
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> </span>&#123;

	<span class="hljs-keyword">private</span> String username;
	<span class="hljs-keyword">private</span> String password;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">User</span><span class="hljs-params">(String username, String password)</span> </span>&#123;
		<span class="hljs-keyword">super</span>();
		<span class="hljs-keyword">this</span>.username = username;
		<span class="hljs-keyword">this</span>.password = password;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getUsername</span><span class="hljs-params">()</span> </span>&#123;
		<span class="hljs-keyword">return</span> username;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUsername</span><span class="hljs-params">(String username)</span> </span>&#123;
		<span class="hljs-keyword">this</span>.username = username;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getPassword</span><span class="hljs-params">()</span> </span>&#123;
		<span class="hljs-keyword">return</span> password;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPassword</span><span class="hljs-params">(String password)</span> </span>&#123;
		<span class="hljs-keyword">this</span>.password = password;
	&#125;

&#125;</code></pre></div>
</li>
<li><p><em>login.jsp : 用户登录页面.</em></p>
<div class="hljs"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">%@</span> <span class="hljs-attr">page</span> <span class="hljs-attr">language</span>=<span class="hljs-string">"java"</span> <span class="hljs-attr">contentType</span>=<span class="hljs-string">"text/html; charset=UTF-8"</span>
    <span class="hljs-attr">pageEncoding</span>=<span class="hljs-string">"UTF-8"</span>%&gt;</span>
<span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>应用案例 : Filter实现用户自动登录<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
	<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">align</span>=<span class="hljs-string">"center"</span>&gt;</span>用户登录页面<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">hr</span>/&gt;</span>
			<span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">action</span>=<span class="hljs-string">"LoginServlet"</span> <span class="hljs-attr">method</span>=<span class="hljs-string">"post"</span>&gt;</span>
				<span class="hljs-tag">&lt;<span class="hljs-name">table</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"380px"</span> <span class="hljs-attr">cellpadding</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">align</span>=<span class="hljs-string">"center"</span>&gt;</span>
					 <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
					 	<span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">align</span>=<span class="hljs-string">"center"</span>&gt;</span>用户名<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
					 	<span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"userName"</span>/&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
					 <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
					 <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
					 	<span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">align</span>=<span class="hljs-string">"center"</span>&gt;</span>密&amp;nbsp;码<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
					 	<span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"password"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"userPassword"</span>/&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
					 <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
					 <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
					 	<span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"40"</span> <span class="hljs-attr">align</span>=<span class="hljs-string">"center"</span>&gt;</span>自动登录时间<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
					 	<span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>
					 		<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"radio"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"autologin"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"$&#123;60*60 &#125;"</span>/&gt;</span>一小时
					 		<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"radio"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"autologin"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"$&#123;60*60*24 &#125;"</span>/&gt;</span>一天
					 		<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"radio"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"autologin"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"$&#123;60*60*24*7 &#125;"</span>/&gt;</span>一周
					 		<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"radio"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"autologin"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"$&#123;60*60*24*31 &#125;"</span>/&gt;</span>一个月
					 	<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
					 <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
					 <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
					 	<span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"50"</span> <span class="hljs-attr">colspan</span>=<span class="hljs-string">"2"</span> <span class="hljs-attr">align</span>=<span class="hljs-string">"center"</span>&gt;</span>
					 		<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"登录"</span>/&gt;</span>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
					 		<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"reset"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"重置"</span>/&gt;</span>
					 		<span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"font-size:13px;color:red"</span>&gt;</span>$&#123;errorMsg &#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
					 	<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
					 <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
				<span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span>
			<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
	<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></code></pre></div>
</li>
<li><p><em>LoginServlet.java : 处理用户的登录请求.</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.filter.example.autologin;

<span class="hljs-keyword">import</span> java.io.IOException;
<span class="hljs-keyword">import</span> javax.servlet.ServletException;
<span class="hljs-keyword">import</span> javax.servlet.annotation.WebServlet;
<span class="hljs-keyword">import</span> javax.servlet.http.Cookie;
<span class="hljs-keyword">import</span> javax.servlet.http.HttpServlet;
<span class="hljs-keyword">import</span> javax.servlet.http.HttpServletRequest;
<span class="hljs-keyword">import</span> javax.servlet.http.HttpServletResponse;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: LoginServlet
 * <span class="hljs-doctag">@Description</span>: 处理用户的登录请求
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 1, 2019 9:38:38 PM
 *
 */</span>
<span class="hljs-meta">@WebServlet</span>(<span class="hljs-string">"/LoginServlet"</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LoginServlet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">HttpServlet</span> </span>&#123;
	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = <span class="hljs-number">1L</span>;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">LoginServlet</span><span class="hljs-params">()</span> </span>&#123;
		<span class="hljs-keyword">super</span>();
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doGet</span><span class="hljs-params">(HttpServletRequest request, HttpServletResponse response)</span>
			<span class="hljs-keyword">throws</span> ServletException, IOException </span>&#123;

		String name = request.getParameter(<span class="hljs-string">"userName"</span>);
		String password = request.getParameter(<span class="hljs-string">"userPassword"</span>);
		String autoLogin = request.getParameter(<span class="hljs-string">"autologin"</span>);

		<span class="hljs-comment">// 检查用户登录信息</span>
		<span class="hljs-keyword">if</span> (<span class="hljs-string">"YUbuntu0109"</span>.equals(name) &amp;&amp; <span class="hljs-string">"demo"</span>.equals(password)) &#123;

			<span class="hljs-comment">// 将用户对象存入session域</span>
			User user = <span class="hljs-keyword">new</span> User(name, password);
			request.getSession().setAttribute(<span class="hljs-string">"user"</span>, user);

			<span class="hljs-keyword">if</span> (autoLogin != <span class="hljs-keyword">null</span>) &#123;
				Cookie cookie = <span class="hljs-keyword">new</span> Cookie(<span class="hljs-string">"autologin"</span>, name + <span class="hljs-string">"-"</span> + password);<span class="hljs-comment">// 加密</span>
				cookie.setMaxAge(Integer.parseInt(autoLogin));
				cookie.setPath(request.getContextPath());
				response.addCookie(cookie);<span class="hljs-comment">// 发送自动登录的cookie</span>
			&#125;
			response.sendRedirect(<span class="hljs-string">"index.jsp"</span>); <span class="hljs-comment">// 跳转到首页</span>

		&#125; <span class="hljs-keyword">else</span> &#123;
			request.setAttribute(<span class="hljs-string">"errorMsg"</span>, <span class="hljs-string">"提示: 用户名或密码错误 !"</span>);
			request.getRequestDispatcher(<span class="hljs-string">"/login.jsp"</span>).forward(request, response);
		&#125;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doPost</span><span class="hljs-params">(HttpServletRequest request, HttpServletResponse response)</span>
			<span class="hljs-keyword">throws</span> ServletException, IOException </span>&#123;
		doGet(request, response);
	&#125;

&#125;</code></pre></div>
</li>
<li><p><em>LoginServlet.java : 注销用户的登录信息.</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.filter.example.autologin;

<span class="hljs-keyword">import</span> java.io.IOException;
<span class="hljs-keyword">import</span> javax.servlet.ServletException;
<span class="hljs-keyword">import</span> javax.servlet.annotation.WebServlet;
<span class="hljs-keyword">import</span> javax.servlet.http.Cookie;
<span class="hljs-keyword">import</span> javax.servlet.http.HttpServlet;
<span class="hljs-keyword">import</span> javax.servlet.http.HttpServletRequest;
<span class="hljs-keyword">import</span> javax.servlet.http.HttpServletResponse;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: LogoutServlet
 * <span class="hljs-doctag">@Description</span>: 注销用户登录的信息
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 1, 2019 9:47:49 PM
 *
 */</span>
<span class="hljs-meta">@WebServlet</span>(<span class="hljs-string">"/LogoutServlet"</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LogoutServlet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">HttpServlet</span> </span>&#123;
	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = <span class="hljs-number">1L</span>;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">LogoutServlet</span><span class="hljs-params">()</span> </span>&#123;
		<span class="hljs-keyword">super</span>();
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doGet</span><span class="hljs-params">(HttpServletRequest request, HttpServletResponse response)</span>
			<span class="hljs-keyword">throws</span> ServletException, IOException </span>&#123;

		<span class="hljs-comment">// 将Session会话中保存的User对象删除</span>
		request.getSession().removeAttribute(<span class="hljs-string">"user"</span>);

		<span class="hljs-comment">// 将自动登录的Cookie删除</span>
		Cookie cookie = <span class="hljs-keyword">new</span> Cookie(<span class="hljs-string">"autologin"</span>, <span class="hljs-string">"msg"</span>);
		cookie.setPath(request.getContextPath());
		cookie.setMaxAge(<span class="hljs-number">0</span>);
		response.addCookie(cookie);
		response.sendRedirect(request.getContextPath() + <span class="hljs-string">"/index.jsp"</span>);
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doPost</span><span class="hljs-params">(HttpServletRequest request, HttpServletResponse response)</span>
			<span class="hljs-keyword">throws</span> ServletException, IOException </span>&#123;
		doGet(request, response);
	&#125;

&#125;</code></pre></div>
</li>
<li><p><em>AutoLoginFilter.java : 拦截用户登录的访问请求.</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.filter.example.autologin;

<span class="hljs-keyword">import</span> java.io.IOException;

<span class="hljs-keyword">import</span> javax.servlet.Filter;
<span class="hljs-keyword">import</span> javax.servlet.FilterChain;
<span class="hljs-keyword">import</span> javax.servlet.FilterConfig;
<span class="hljs-keyword">import</span> javax.servlet.ServletException;
<span class="hljs-keyword">import</span> javax.servlet.ServletRequest;
<span class="hljs-keyword">import</span> javax.servlet.ServletResponse;
<span class="hljs-keyword">import</span> javax.servlet.annotation.WebFilter;
<span class="hljs-keyword">import</span> javax.servlet.http.Cookie;
<span class="hljs-keyword">import</span> javax.servlet.http.HttpServletRequest;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: AutoLoginFilter
 * <span class="hljs-doctag">@Description</span>: 拦截用户登录的访问请求
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 1, 2019 9:51:23 PM
 *
 */</span>
<span class="hljs-meta">@WebFilter</span>(filterName = <span class="hljs-string">"/AutoLoginFilter"</span>, urlPatterns = <span class="hljs-string">"/*"</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AutoLoginFilter</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Filter</span> </span>&#123;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">AutoLoginFilter</span><span class="hljs-params">()</span> </span>&#123;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">destroy</span><span class="hljs-params">()</span> </span>&#123;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doFilter</span><span class="hljs-params">(ServletRequest request, ServletResponse response, FilterChain chain)</span>
			<span class="hljs-keyword">throws</span> IOException, ServletException </span>&#123;

		HttpServletRequest request2 = (HttpServletRequest) request;
		Cookie[] cookies = request2.getCookies();

		<span class="hljs-comment">// 获取一个名为`autologin`的cookie</span>
		String autoLogin = <span class="hljs-keyword">null</span>;
		<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; cookies != <span class="hljs-keyword">null</span> &amp;&amp; i &lt; cookies.length; i++) &#123;
			<span class="hljs-keyword">if</span> (<span class="hljs-string">"autologin"</span>.equals(cookies[i].getName())) &#123;
				autoLogin = cookies[i].getValue();
				<span class="hljs-keyword">break</span>;
			&#125;
		&#125;

        <span class="hljs-comment">// 验证用户登录信息</span>
		<span class="hljs-keyword">if</span> (autoLogin != <span class="hljs-keyword">null</span>) &#123;
			String[] parts = autoLogin.split(<span class="hljs-string">"-"</span>);
			String name = parts[<span class="hljs-number">0</span>];
			String password = parts[<span class="hljs-number">1</span>];
			<span class="hljs-keyword">if</span> (<span class="hljs-string">"YUbuntu0109"</span>.equals(name) &amp;&amp; <span class="hljs-string">"demo"</span>.equals(password)) &#123;
                <span class="hljs-comment">// 将用户的登录信息封装到User对象并存入Sesssion域中,既而完成自动登录</span>
				User user = <span class="hljs-keyword">new</span> User(name, password);
				request2.getSession().setAttribute(<span class="hljs-string">"user"</span>, user);
			&#125;
		&#125;

		chain.doFilter(request, response);
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">(FilterConfig fConfig)</span> <span class="hljs-keyword">throws</span> ServletException </span>&#123;
	&#125;

&#125;</code></pre></div>
</li>
</ol>

            </article>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Java-web/">Java web</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" target="_blank" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <div class="post-prev col-6">
                    
                    
                      <a href="/2019/05/02/Java-web-Servlet-listener/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Java web:Servlet listener</span>
                        <span class="visible-mobile">Previous</span>
                      </a>
                    
                  </div>
                  <div class="post-next col-6">
                    
                    
                      <a href="/2019/05/02/Java-web-%E8%AF%B7%E6%B1%82%E4%B8%8E%E5%93%8D%E5%BA%94/">
                        <span class="hidden-mobile">Java web:请求与响应</span>
                        <span class="visible-mobile">Next</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </div>
                </div>
              
            </div>

            
          </div>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;TOC</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">Search</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">keyword</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
    <div>
      <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a>
      <i class="iconfont icon-love"></i>
      <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener">
        <span>Fluid</span></a>
    </div>
    
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


    

    
  </div>
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: 'article.markdown-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "Java web:Filter&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script>
      !function (e, t, a) {
        function r() {
          for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
          requestAnimationFrame(r)
        }

        function n() {
          var t = "function" == typeof e.onclick && e.onclick;
          e.onclick = function (e) {
            t && t(), o(e)
          }
        }

        function o(e) {
          var a = t.createElement("div");
          a.className = "heart", s.push({
            el: a,
            x: e.clientX - 5,
            y: e.clientY - 5,
            scale: 1,
            alpha: 1,
            color: c()
          }), t.body.appendChild(a)
        }

        function i(e) {
          var a = t.createElement("style");
          a.type = "text/css";
          try {
            a.appendChild(t.createTextNode(e))
          } catch (t) {
            a.styleSheet.cssText = e
          }
          t.getElementsByTagName("head")[0].appendChild(a)
        }

        function c() {
          return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
        }

        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
          setTimeout(e, 1e3 / 60)
        }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
      }(window, document);
    </script>
  














</body>
</html>
